//
//  BaseViewController.m
//  BaseAPP
//
//  Created by Evisu on 16/12/2.
//  Copyright © 2016年 evisum. All rights reserved.
//

#import "BaseViewController.h"
#import "DMLoginViewController.h"

@interface BaseViewController ()

//状态栏(上下移显示/消失)
@property (nonatomic, strong)UIView *statusBarView;

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = KBACKGROUND_COLOR;
    
    //适配iOS7或以上，取消setAutomaticallyAdjustsScrollViewInsets自动调整
    //否则布局会有空白
    if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
        
        [self setAutomaticallyAdjustsScrollViewInsets:NO];
    }

}

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    
    //从其他设备登录
    [BaseNotificationCenter addObserver:self selector:@selector(loginFromOtherDevice) name:KLOGIN_FROM_OTHER_DEVICE object:nil];
    
    //账号被服务器删除
    [BaseNotificationCenter addObserver:self selector:@selector(accountError) name:KACCOUNT_ERROR object:nil];
    
}

- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];
    
    [BaseNotificationCenter removeObserver:self name:KLOGIN_FROM_OTHER_DEVICE object:nil];
    
    [BaseNotificationCenter removeObserver:self name:KACCOUNT_ERROR object:nil];
    
}

- (UIView *)statusBarView {

    if (!_statusBarView) {
        
        _statusBarView = [[UIView alloc] init];
    }
    
    return _statusBarView;
}

- (void)showLeftBarButtonWithImageName:(NSString *)imageName {

    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    leftBtn.frame = CGRectMake(0, 0, 30, 30);
    
    UIImage *addImage = [UIImage imageNamed:imageName];
    
    addImage = [addImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [leftBtn setBackgroundImage:addImage forState:UIControlStateNormal];
    
    UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    self.navigationItem.leftBarButtonItem = leftBarBtn;
    
    [leftBtn addTarget:self action:@selector(didClickedLeftBtn) forControlEvents:UIControlEventTouchUpInside];
}

- (void)showRightBarButtonWithImageName:(NSString *)imageName {

    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    rightBtn.frame = CGRectMake(0, 0, 30, 30);
    
    UIImage *addImage = [UIImage imageNamed:imageName];
    
    addImage = [addImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [rightBtn setBackgroundImage:addImage forState:UIControlStateNormal];
    
    UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    self.navigationItem.rightBarButtonItem = rightBarBtn;
    
    [rightBtn addTarget:self action:@selector(didClickedRightBtn) forControlEvents:UIControlEventTouchUpInside];
}

/**
 *  点击左边barButtonItem
 */
- (void)didClickedLeftBtn {
    
    if (self.leftBtnBlock) {
        
        self.leftBtnBlock();
    }
}

/**
 *  点击右边barButtonItem
 */
- (void)didClickedRightBtn {
    
    if (self.rightBtnBlock) {
        
        self.rightBtnBlock();
    }
}

- (void)hideNavigationBar {

    //导航栏往上移
    [UIView animateWithDuration:0.3 animations:^{
        
        self.navigationController.navigationBar.top_sd = -KSTATUS_AND_NAVIGATION_HEIGHT;
        
    } completion:^(BOOL finished) {
        
        //添加状态栏View，改变状态栏的颜色
        self.statusBarView.frame = CGRectMake(0, KSTATUS_AND_NAVIGATION_HEIGHT, self.view.bounds.size.width, KSTATUSBAR_HEIGHT);
        self.statusBarView.backgroundColor = HEXColor(@"EFEEF4");
        [self.navigationController.navigationBar addSubview:self.statusBarView];
        
    }];
    
    //改变状态栏样式
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    
}

- (void)showNavigationBar {

    //导航栏往下移
    [UIView animateWithDuration:0.3 animations:^{
        
        self.navigationController.navigationBar.top_sd = KSTATUSBAR_HEIGHT;
    }];
    
    //改变状态栏样式
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    //移除状态栏View
    [self.statusBarView removeFromSuperview];
}

- (void)setAutomaticallyAdjustsScrollViewInsets:(BOOL)automaticallyAdjustsScrollViewInsets {

    if (IOS_VERSION_7_OR_LATER) {
        [super setAutomaticallyAdjustsScrollViewInsets:automaticallyAdjustsScrollViewInsets];
    }
}

#pragma mark - 环信回调
//被动退出登录-在其他地方登录
- (void)loginFromOtherDevice {

    [MBProgressHUD showError:@"账号已在另一台设备登录" toView:self.view];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        DMLoginViewController *loginVC = [[DMLoginViewController alloc] init];
        [UIApplication sharedApplication].keyWindow.rootViewController = loginVC;
    });
    
}

//被动退出登录-账号被服务器删除
- (void)accountError {

    [MBProgressHUD showError:@"账号异常，请从新登录" toView:self.view];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        DMLoginViewController *loginVC = [[DMLoginViewController alloc] init];
        [UIApplication sharedApplication].keyWindow.rootViewController = loginVC;
    });
}


@end
